// pages/material/index.js
const app = getApp() // 引入app
const _request = require("../../utils/request.js"); // 引入require;
const {
  isNull
} = require("../../utils/util.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    baseurl: app.globalData.baseurl,
    imgUrl: app.globalData.imgUrl, //全局img路径
    shopimgUrl: app.globalData.shopimgUrl, 
    userObj: null,
    servicenames: null,
    isSexShow: false,
    sexs: [
      ['男', '女']
    ],
    showStoreNameModal: false,
    storeName: '',
    showStationModal: false,
    storeStation: '',
    storeNameLength: 0
  },

  // 获取用户个人信息
  getStoreCenterInfo(){
    _request.get('/storeCenterInfo/getStoreCenterInfo', {
      id: wx.getStorageSync('userid')
    }).then((res) => {
      this.setData({
        userObj: res.data.data,
      });
    }).catch((error) => {
      console.log(error, 'error');
    })
  },
 
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    // 获取用户个人信息
    this.getStoreCenterInfo()
  },
  // 头像
  afterRead1() {
    let _this = this
    wx.chooseImage({
      success(res) {
        console.log(res.tempFilePaths[0],'res');
        const tempFilePaths = res.tempFilePaths
        wx.uploadFile({
          url: _this.data.baseurl + '/store-api/file/batchUpload',
          filePath: tempFilePaths[0],
          name: 'files',
          header: {
            'token': wx.getStorageSync('token') || null
          },
          formData: {
            'file': 'file'
          },
          success(res) {
            console.log(res.data);
            const headImg = JSON.parse(res.data).data[0]
            console.log(headImg,'headImg');
            _request.postObj('/storeCenterInfo/updateHeadImg', {
              userId: wx.getStorageSync('userId'),
              headImg: headImg,
            }).then((res) => {
              if (res.data.code === 200) {
                wx.showToast({
                  title: '修改成功',
                  icon: 'success',
                  duration: 2000
                })
                _this.getStoreCenterInfo()
              } else {
                wx.showToast({
                  title: res.data.message,
                  icon: 'none',
                  duration: 2000
                })
              }
            }).catch((error) => {
              console.log(error, 'error');
            })
          },
          fail(error) {
            console.log(error);
          },
          complete(res) {
            console.log(res);
          },
        })
      }
    })
  },
  showStoreNameBtn: function () {
    this.setData({
      showStoreNameModal: true
    })
  },

  hideStoreNameModal: function () {
    this.setData({
      storeNameLength:0,
      storeName: ''
    })
    this.setData({
      showStoreNameModal: false
    });
  },

  onStoreNameCancel: function () {
    this.hideStoreNameModal();
  },

  onStoreNameConfirm: function () {
    var storeName = this.data.storeName;
    if(!storeName || isNull(storeName)){
      wx.showToast({
        title: '门店名不能为空',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    _request.postObj('/storeCenterInfo/updateStoreName', {
      storeId: wx.getStorageSync('storeId'),
      storeName: this.data.storeName,
    }).then((res) => {
      if (res.data.code === 200) {
        let userObj = this.data.userObj;
        userObj.storeName = this.data.storeName;
        this.setData({
          userObj: userObj
        });
        this.hideStoreNameModal();
        wx.showToast({
          title: '修改成功',
          icon: 'success',
          duration: 2000
        })
      } else {
        this.hideStoreNameModal();
        wx.showToast({
          title: res.data.message,
          icon: 'none',
          duration: 2000
        })
      }
    }).catch((error) => {
      console.log(error, 'error');
    })
  },
  // 卡位
  stationblur: function (e) {
    this.setData({
      storeStation: e.detail.value
    })
  },
  showStationBtn: function () {
    this.setData({
      showStationModal: true
    })
  },

  hideStationModal: function () {
    this.setData({
      storeStation: ''
    })
    this.setData({
      showStationModal: false
    });
  },

  onStationCancel: function () {
    this.hideStationModal();
  },

  onStationConfirm: function () {
    if(!this.data.storeStation || isNull(this.data.storeStation)){
      wx.showToast({
        title: '卡位不能为空',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    _request.postObj('/storeCenterInfo/updateStation', {
      storeId: wx.getStorageSync('storeId'),
      storeStation: this.data.storeStation.trim(),
    }).then((res) => {
      if (res.data.code === 200) {
        let userObj = this.data.userObj;
        userObj.station = this.data.storeStation.trim();
        this.setData({
          userObj: userObj
        });
        this.hideStationModal();
        wx.showToast({
          title: '修改成功',
          icon: 'success',
          duration: 2000
        })
      } else {
        this.hideStationModal();
        wx.showToast({
          title: res.data.message,
          icon: 'none',
          duration: 2000
        })
      }

    }).catch((error) => {
      console.log(error, 'error');
    })
  },
  // 地址
  onSite() {
    wx.navigateTo({
      url: '/pages/myprofile/site/index',
    })
  },
  // 性别
  showSex: function () {
    this.setData({
      isSexShow: true
    })
  },
  sure(e) {
    this.setData({
      isSexShow: false,
    })
    var sex = e.detail.choosedData[0] == '男' ? 1 : 2;
    _request.postObj('/storeCenterInfo/updateUserSex', {
      userId: wx.getStorageSync('userId'),
      sex: sex,
    }).then((res) => {
      if (res.data.code === 200) {
        wx.showToast({
          title: '修改成功',
          icon: 'success',
          duration: 2000
        })
      } else {
        wx.showToast({
          title: res.data.message,
          icon: 'none',
          duration: 2000
        })
      }
    }).catch((error) => {
      console.log(error, 'error');
    })
  },
  cancle() {
    this.setData({
      isSexShow: false,
    })
  },
  // 环境图
  onEnvironment(){
    wx.navigateTo({
      url: '/pages/myprofile/environment/index',
    })
  },
  // 服务范围
  onServices(){
    wx.navigateTo({
      url: '/pages/myprofile/services/index',
    })
  },
  openChooseImage() {
    wx.chooseImage({
      count: 1,
      success: function() {
        // 缺少头像上传接口
      }
    })
  },
  handleinput(event) {
    this.setData({
      storeNameLength: event.detail.value.length
    })
    
  }

})